//
//  messageView.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/8/16.
//  Copyright © 2016年 www.hd123.com. All rights reserved.
//

import UIKit

class MessageView: UIView {

    //MARK: - property
    weak var controller:UITableViewController?
    lazy fileprivate var imageLeft:UIImageView = {
        let imageView = UIImageView()
        return imageView
    }()
    lazy var mesageLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        label.alpha = 0.2
        label.numberOfLines = 3
        label.textColor = UIColor.hexStringToUIColor("#666666")
        return label
    }()
    lazy fileprivate var clearButton:UIButton = {
        let button = UIButton()
        return button
    }()
    //MARK: - life cycle
    override func didMoveToSuperview() {
        UIView.animate(withDuration: 0.2, animations: { 
            self.mesageLabel.alpha = 1
        }) 
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    init(){
        super.init(frame: CGRect.zero)
    }
    convenience init(onVC: UITableViewController,message: String, success: Bool){
        self.init()
        self.controller = onVC
        self.clearButton.addTarget(self, action: #selector(MessageView.clickClearButton(_:)), for: UIControlEvents.touchUpInside)
        self.backgroundColor = UIColor.hexStringToUIColor("#f9e8c9")
        let options : NSStringDrawingOptions = .usesLineFragmentOrigin
        let boundingRect = (message as NSString).boundingRect(with: CGSize(width: KscreenWidth - 90, height: 0), options: options, attributes: [NSFontAttributeName:mesageLabel.font], context: nil)
        let height = boundingRect.height < 38 ? 38 : boundingRect.height
        let y = 64 - height
        self.frame = CGRect(x: 0, y: y, width: KscreenWidth, height: height)
        if success == true{
            self.imageLeft.image = UIImage(named: "success")
        }else{
            self.imageLeft.image = UIImage(named: "warning")
        }
        self.clearButton.setImage(UIImage(named: "closed"), for: UIControlState())
        self.mesageLabel.text = message
        //布局
        self.addSubview(self.imageLeft)
        self.addSubview(self.mesageLabel)
        self.addSubview(self.clearButton)
        self.imageLeft.snp.makeConstraints { (make) in
            make.leading.equalTo(self).offset(10)
            make.centerY.equalTo(self)
        }
         self.mesageLabel.snp.makeConstraints { (make) in
            make.leading.equalTo(self).offset(50)
            make.centerY.equalTo(self)
            make.trailing.equalTo(self).offset(-40)
        }
        self.clearButton.snp.makeConstraints { (make) in
//            make.leading.equalTo(self.mesageLabel.snp.trailing).offset(5)
            make.centerY.equalTo(self)
            make.trailing.equalTo(self).offset(-10)
            make.width.equalTo(44)
            make.height.equalTo(44)
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK: - IBAction
    func clickClearButton(_ button: UIButton){
        HDShowMessageManager.cancelPreviousPerformRequests(withTarget: HDShowMessageManager.sharedManager, selector: #selector(HDShowMessageManager.messageAnimation(_ :)), object: self)
        UIView.animate(withDuration: 1, animations: {
            self.y -= 40
            let tableView = self.controller?.view as! UITableView
            tableView.contentOffset.y = tableView.contentOffset.y + self.height
//            if self.controller?.tableView.contentSize.height > KscreenHeight - (self.controller?.tableView.contentInset.top)! - (self.controller?.tableView.contentInset.bottom)! {
                self.controller?.tableView.contentInset.top = 64
//            }
        }, completion: { (true) in
            self.removeFromSuperview()
        }) 
    }
    //MARK: - private

}
